package com.euq.utils;
import com.euq.entity.Dict;

import java.util.ArrayList;
import java.util.List;

public class AddDictItems {
    public static List<Dict> parseDictItems(String value, Long dictId) {
        value = cleanValueString(value);
        List<Dict> result = new ArrayList<>();

        String[] entries = value.split(";");
        for (String entry : entries) {
            String[] parts = entry.split(":");
            if (parts.length == 2) {

                String num = parts[0].trim();
                String name = parts[1].trim();
                if (BeanUtils.isOneEmpty(num, name)) {
                    throw new RuntimeException("参数不能为空");
                }

                Dict item = new Dict();
                item.setPid(dictId);
                item.setNum(num);
                item.setName(name);
                item.setTips(name);

                result.add(item);
            }
        }

        return result;
    }

    private static String cleanValueString(String value) {
        if (value == null) return "";
        return value.trim().replaceAll("\\s+", " ").replaceAll("\\s*:\\s*", ":").replaceAll("\\s*;\\s*", ";");
    }
}
